package com.font.web.zhongyao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.font.bean.*;
import com.font.bean.result.Result;
import com.font.service.DaService;
import com.font.service.DianzanService;
import com.font.service.PrescriptionService;
import com.font.service.WenService;
import com.font.utils.PageListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/system")
public class wendaController {

    @Autowired
    DianzanService dianzanService;
    @Autowired
    WenService wenService;
    @Autowired
    DaService daService;

    @GetMapping("list")
    public Result prescriptionList(
            @RequestParam(value = "wenKey", required = false) String wenKey,
            @RequestParam(value = "wenContent", required = false) String wenContent,
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "4") Integer pageSize,
            @RequestParam(value = "userName", required = false) String userName
    ) {
        List<Wen> wenList = wenService.preList(wenContent, wenKey, currentPage, pageSize);
        for (Wen wen : wenList) {
            System.err.println(wen);
            wen.setDas(daService.dalist(wen.getWenKey()));
        }
        System.err.println(wenList);
        IPage<Wen> prescriptionIPage = PageListUtils.listToPage(wenList, currentPage, pageSize);

        return Result.success(prescriptionIPage);
    }


    @GetMapping("insert")
    public Result insert(
            @RequestParam(value = "wenKey", required = false) String wenKey,
            @RequestParam(value = "daFrom", required = false) String daFrom,
            @RequestParam(value = "daContent", required = false) String daContent,
            @RequestParam(value = "avatar", required = false) String avatar
    ) {

        Wen byId = wenService.getById(wenKey);
        Wen wen = new Wen();
        wen.setWenKey(wenKey); wen.setWenTypeId(byId.getWenTypeId()+1);
        wenService.updateById(wen);
        int a = wenService.insert(wenKey, daFrom, daContent, avatar);
        return Result.success(a);
    }

    @GetMapping("weninsert")
    public Result weninsert(
            @RequestParam(value = "wenFrom", required = false) String wenFrom,
            @RequestParam(value = "wenContent", required = false) String wenContent
    ) {
        int a = wenService.weninsert(wenFrom, wenContent);
        return Result.success(a);
    }

    @GetMapping("dianzan")
    @Transactional
    public Result dianzan(
            @RequestParam(value = "visable", required = false) Boolean visable,
            @RequestParam(value = "daKey", required = false) String daKey,
            @RequestParam(value = "userName", required = false) String userName
    ) {
        Da da = new Da();
        da.setDaKey(daKey);
        Integer nice = daService.getById(daKey).getNice();
        System.err.println(nice);
        if (!visable) {
            da.setNice(nice + 1);
        } else {
            da.setNice(nice - 1);
        }
        boolean b1 = daService.updateById(da);

        Dianzan one = dianzanService.getOne(new LambdaQueryWrapper<Dianzan>().eq(Dianzan::getUserName, userName).eq(Dianzan::getDaKey, daKey));
        if (StringUtils.isEmpty(one)) {
            Dianzan dianzan = new Dianzan(UUID.randomUUID().toString(), userName, daKey, !visable);
            int c = wenService.dianZanInsert(dianzan);
        } else {
            Dianzan dianzan = new Dianzan(one.getDiankey(), userName, daKey, !visable);
            boolean b = dianzanService.updateById(dianzan);
        }
        return Result.success();
    }


    @GetMapping("/bankList")
    public Result bankList(String name) {
        System.err.println(name);
        List<Wen> list = wenService.list(  new LambdaQueryWrapper<Wen>().eq(Wen::getWenFrom,name));
//        for (Wen wen : list) {
//            List<Da> dalist = daService.dalist(wen.getWenKey());
//            wen.setWenTypeId(dalist.size());
//        }

        return Result.success(list);
    }

    @GetMapping("/updateWenId")
    public Result updateWenId(String wenKey) {
        Wen wen = new Wen();
        wen.setWenKey(wenKey); wen.setWenTypeId(0);
        boolean b = wenService.updateById(wen);
        return Result.success(b);
    }
}
